<?php
/**
 * Created by qinpeizhou.
 * Date: 2018/5/12
 * Time: 14:01
 * Email : 1031219129@qq.com
 * Yaf 控制器基类
 */
include_once( dirname(__FILE__).'/../library/Aes.php' );
include_once( dirname(__FILE__).'/../library/exception/SignException.php' );
include_once( dirname(__FILE__).'/../library/exception/SignFailException.php' );



/**
 * API模块,功能控制器
 */
class Base_Controller extends Yaf_Controller_Abstract{
    
    /**
     * header头信息
     * @var string
     */
    public $headers = '';
    /**
     * 初始化的方法
     */
    public function _initialize(){
        parent::_initialize(); // TODO: 改变自动生成的存根
        //$this->testAes();
//        $this->checkRequestAuth();

    }

    /**
     * 检查每次app请求的数据是否合法
     */
    public function checkRequestAuth(){
        // 获取请求头
        $request = $this->getRequest();
        // 校验sign是否存在
        if(empty($request->getParam('sign'))){
            throw new SignException();
        }

        /*// app_type校验 移动端接口专用
        if(in_array($header['app_type'],config('secure.apptypes'))){
            throw new AppTypeException();
        }*/

        // 检测sign是否合法
        if(!IAuth::checkSignPass($request->getParams())){
            throw new SignFailException();
        }
        $this->headers = $request->getParams();

    }


    public function testAes(){
        /*// aes加密
        $str = "aavv2333455dd33";
        echo (new Aes())->encrypt($str); exit;*/
        /* // aes解密
         $str = "jhf8ZvOiA1jC+fJgdmXe8EJtM3ltYmg0aWFETGc1WVFsenQra0dnQ3M5TDY2aXplc2pudjlIT2NueGs9";
         echo (new Aes())->decrypt($str); exit;*/
        $data = [
            'did' => 'aavv23',
            "version" => 1,
            'sign' => "cGVpemhvdTExMHNzMTFycm9KUmI0Q1VrRWNBVWRpTDNyT1J2Wnc9PQ=="
        ];
        echo IAuth::setSign($data);exit;    // AES封装加密

    }
}
